
package com.yadong.smarthome2.protocol2.client;

import java.util.List;

import com.yadong.smarthome2.protocol2.Protocol;
import com.yadong.smarthome2.protocol2.RequestProtocol.Request;
import com.yadong.smarthome2.protocol2.ResultProtocol.DeviceInfo;
import com.yadong.smarthome2.protocol2.ResultProtocol.DeviceInfo.DeviceType;
import com.yadong.smarthome2.protocol2.ResultProtocol.DeviceInfo.Status;
import com.yadong.smarthome2.protocol2.ResultProtocol.Result;

public abstract class DeviceInfoManager {
    private FunctionManager mFunctionManager;

    public DeviceInfoManager(FunctionManager functionManager) {
        this.mFunctionManager = functionManager;
    }

    protected abstract DeviceType getDeviceType();

    protected abstract String getDeviceDescription();

    protected abstract List<Status> getStatusInfo();

    protected abstract int getCurrentDeviceInfoVersion();

    public boolean handleDeviceInfoRequest(Request request, SendResultInterface service) {
        if (Protocol.isDeviceInfoResult(request.getTag())) {
            service.sendResult(createDeviceInfoListRequest(request));
            return true;
        }
        return false;
    }

    private Result createDeviceInfoListRequest(Request request) {
        DeviceInfo.Builder deviceInfoBuidler = DeviceInfo.newBuilder();
        deviceInfoBuidler.setType(getDeviceType());
        deviceInfoBuidler.setDescription(getDeviceDescription());
        deviceInfoBuidler.addAllStatus(getStatusInfo());
        deviceInfoBuidler.setVersion(getCurrentDeviceInfoVersion());
        deviceInfoBuidler.addAllFunctions(mFunctionManager.getAllFunctions());

        Result.Builder resultBuilder = Result.newBuilder();
        resultBuilder.setTag(request.getTag());
        resultBuilder.setTime(request.getTime());
        resultBuilder.addResult(Protocol.RESULT_OK);
        resultBuilder.setDeviceInfo(deviceInfoBuidler.build());
        return resultBuilder.build();
    }
}
